import { defineStore } from 'pinia';
import { type UserState } from '../interface/index';
import piniaPersistConfig from '@/store/helper/piniaPersist';
import router from '@/router';
import { LOGIN_URL } from '@/config/config';

export const useUserStore = defineStore({
  id: 'user-store',
  state: (): UserState => ({
    token: '',
  }),
  actions: {
    SET_TOKEN(token: string) {
      this.token = token;
    },
    LOG_OUT() {
      this.token = '';
      router.replace(LOGIN_URL);
    },
  },
  persist: piniaPersistConfig('UserStore'),
});
